package org.itboys.core.service.mongo.front;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dto.account.SundayAccountDto;
import org.itboys.core.dto.advertisment.SundayAdvertisementDto;
import org.itboys.core.entity.mongo.SundayAccount;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/11/27
 * @Description：账户
 */
@Service
public class SundayFrontAccountService extends BaseMongoService<SundayAccount>{
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayAccount> getEntityClass() {
        return SundayAccount.class;
    }
    @Autowired
    private SundayValidateCodeService codeService;
    @Autowired
    private SundayFrontAdvertisementService advertisementService;

    /**
     * 保存账户
     * @param accountId
     * @param memberId
     * @param bankType
     * @param realName
     * @param mobile
     * @param code
     * @param number
     */
    public void saveAccount(Long accountId, Long memberId, Integer bankType,String realName, String mobile, String code, String number) {
        codeService.checkValidateCode(mobile,code);
        if (LongUtil.isNotZreo(accountId)) {
            SundayAccount account = super.getById(accountId);
            if (account.getMemberId().longValue() != memberId.longValue()) {
                throw new ErrorException("账户非当前用户");
            }
            account.setBankType(bankType);
            account.setRealName(realName);
            account.setMobile(mobile);
            account.setNumber(number);
            super.updateExceptEmpty(accountId, account);
        } else {
            super.save(new SundayAccount(memberId, bankType, realName, mobile, number));
        }
    }

    /**
     * 查找单个
     *
     * @param accountId
     * @return
     */
    public SundayAccountDto findOne(Long accountId) {
        List<SundayAdvertisementDto> advertisements = advertisementService.getAdvertisementByZone("bank");
        SundayAccount account = super.getById(accountId);
        SundayAccountDto result = new SundayAccountDto();
        BeanUtils.copyProperties(account, result);
        for (SundayAdvertisementDto dto : advertisements) {
            Integer type = Integer.valueOf(dto.getViceName());
            if (type.equals(account.getBankType())) {
                result.setBankName(dto.getName());
                result.setLogo(dto.getImage());
                break;
            }
        }
        return result;
    }

    /**
     * 查询列表
     *
     * @param memberId
     * @return
     */
    public List<SundayAccountDto> selectAccount(Long memberId) {
        List<SundayAccountDto> results = new ArrayList<SundayAccountDto>();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId", memberId);
        List<SundayAccount> accounts = super.list(param);
        List<SundayAdvertisementDto> advertisements = advertisementService.getAdvertisementByZone("bank");
        if (ListUtil.isNotNull(accounts)) {
            for (SundayAccount account : accounts) {
                SundayAccountDto dto = new SundayAccountDto();
                BeanUtils.copyProperties(account, dto);
                for (SundayAdvertisementDto advertisementDto : advertisements) {
                    Integer type = Integer.valueOf(advertisementDto.getViceName());
                    if (type.equals(account.getBankType()) ) {
                        dto.setBankName(advertisementDto.getName());
                        dto.setLogo(advertisementDto.getImage());
                        break;
                    }
                }
                results.add(dto);
            }
        }
        return results;
    }
}
